package pl.un4giv3n.mightvsmagic.jobs.listener;

import java.text.ParseException;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.quartz.CronTrigger;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

import pl.un4giv3n.mightvsmagic.jobs.RefreshClanRankJob;
import pl.un4giv3n.mightvsmagic.jobs.RefreshClanRankTask;
import pl.un4giv3n.mightvsmagic.jobs.RefreshRankJob;
import pl.un4giv3n.mightvsmagic.jobs.RefreshRankTask;
import pl.un4giv3n.mightvsmagic.jobs.UnblockPlayersJob;
import pl.un4giv3n.mightvsmagic.jobs.UnblockPlayersTask;

public class QuartzSchedulerListener implements ServletContextListener {

	public void contextDestroyed(ServletContextEvent arg0) {
		//
	}

	public void contextInitialized(ServletContextEvent servletContext) {

		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("mightvsmagicds");
		RefreshRankTask rankingTask = new RefreshRankTask(factory
				.createEntityManager());
		RefreshClanRankTask clanRankingTask = new RefreshClanRankTask(factory
				.createEntityManager());

		// specify your scheduler task details
		JobDetail rankingJob = new JobDetail();
		rankingJob.setName(RefreshRankJob.class.getName());
		rankingJob.setJobClass(RefreshRankJob.class);

		JobDataMap dataMap = rankingJob.getJobDataMap();
		dataMap.put(RefreshRankJob.class.getName(), rankingTask);
		
		JobDetail clanRankingJob = new JobDetail();
		clanRankingJob.setName(RefreshClanRankJob.class.getName());
		clanRankingJob.setJobClass(RefreshClanRankJob.class);
		
		JobDataMap data = clanRankingJob.getJobDataMap();
		data.put(RefreshClanRankJob.class.getName(), clanRankingTask);

		UnblockPlayersTask banTask = new UnblockPlayersTask(factory
				.createEntityManager());
		JobDetail banJob = new JobDetail();
		banJob.setName(UnblockPlayersJob.class.getName());
		banJob.setJobClass(UnblockPlayersJob.class);

		JobDataMap banDataMap = banJob.getJobDataMap();
		banDataMap.put(UnblockPlayersJob.class.getName(), banTask);

		try {
			// configure the scheduler time, run it every 5 seconds
			CronTrigger rankTrigger = new CronTrigger();
			rankTrigger.setName(RefreshRankJob.class.getName() + "Trigger");
			rankTrigger.setCronExpression("0 45 * * * ?");
			
			CronTrigger clanRankTrigger = new CronTrigger();
			clanRankTrigger.setName(RefreshClanRankJob.class.getName() + "Trigger");
			clanRankTrigger.setCronExpression("0 59 * * * ?");
			// configure the scheduler time, run it every 5 seconds
			CronTrigger banTrigger = new CronTrigger();
			banTrigger.setName(UnblockPlayersJob.class.getName() + "Trigger");
			banTrigger.setCronExpression("0 30 * * * ?");

			// schedule it
			Scheduler scheduler = new StdSchedulerFactory().getScheduler();
			scheduler.start();
			scheduler.scheduleJob(rankingJob, rankTrigger);
			scheduler.scheduleJob(clanRankingJob, clanRankTrigger);
			scheduler.scheduleJob(banJob, banTrigger);

		} catch (ParseException e) {
			e.printStackTrace();
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
}